package com.topten.util;

public class Util {

	public static String removerMascara(String texto) {

		String retorno = texto.replace("-", "").replace(".", "")
				.replace("(", "").replace(")", "").replace("/", "");

		return retorno;

	}

	// VERIFICA VALIDADE DE CNPJ

	public static boolean verificarCnpj(String cnpj) {
		Integer k = 0, dig = 0, soma = 0;
		String strDig = "", strNum = "";

		try {
			strNum = cnpj.substring(0, cnpj.length() - 2);
		} catch (StringIndexOutOfBoundsException e) {
			return false;
		}
		for (int i = 1; i <= 2; i++) {
			k = 2;
			soma = 0;
			for (int j = strNum.length(); j > 0; j--) {
				try {
					soma += Integer.parseInt(strNum.substring(j - 1, j)) * k;
				} catch (StringIndexOutOfBoundsException e) {
					return false;
				}
				k++;
				if (k > 9) {
					k = 2;
				}
			}
			dig = 11 - soma % 11;
			if (dig >= 10) {
				dig = 0;
			}
			strNum += dig.toString();
		}
		return strNum.compareTo(cnpj) == 0 ? true : false;
	}

	// VERIFICA VALIDADE DE CPF

	public static boolean verificarCpf(String cpf) {
		Integer k = 0, dig = 0, soma = 0;
		String strNum = "";

		try {
			strNum = cpf.substring(0, cpf.length() - 2);
		} catch (StringIndexOutOfBoundsException e) {
			return false;
		}
		for (int i = 1; i <= 2; i++) {
			k = 2;
			soma = 0;
			for (int j = strNum.length(); j > 0; j--) {
				try {
					soma += Integer.parseInt(strNum.substring(j - 1, j)) * k;
				} catch (StringIndexOutOfBoundsException e) {
					return false;
				}
				k++;
			}
			dig = 11 - soma % 11;
			if (dig >= 10) {
				dig = 0;
			}
			strNum += dig.toString();
		}
		return strNum.compareTo(cpf) == 0 ? true : false;
	}

}
